package com.yukino.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yukino.mapper.*;
import com.yukino.pojo.Dept;
import com.yukino.pojo.Member;
import com.yukino.pojo.Progress;
import com.yukino.pojo.requestBody.AddProject;
import com.yukino.pojo.requestBody.UpdateProject;
import com.yukino.pojo.serviceResult.ProjectInfo;
import com.yukino.pojo.Project;
import com.yukino.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private ProgressMapper progressMapper;

    /**
     * 查询项目的完整信息，包括所属部门，成员
     * 基本信息和成员使用多表连接
     * 部门不采用，否则三表以上的连接性能差
     *
     * @return
     */
    @Override
    public List<ProjectInfo> queryInfo() {
        List<ProjectInfo> list = new ArrayList<>();

        List<Project> projects = projectMapper.selectInfo();
        for (Project p : projects) {
            Dept dept = deptMapper.selectById(p.getDept_id());

            ProjectInfo projectInfo = new ProjectInfo(p);
            projectInfo.setDept_name(dept.getDept_name());

            list.add(projectInfo);
        }
        return list;
    }

    /**
     * 条件搜索
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<Project> queryInfoByCondition(Map<String, String> map) {
        QueryWrapper<Project> wrapper = new QueryWrapper<>();
        wrapper.allEq(map);

        return projectMapper.selectList(wrapper);
    }

    /**
     * 新增一个项目
     *
     * @param project 前端异步发送的请求数据
     * @return 插入数量
     */
    @Override
    @Transactional
    public int addOne(AddProject project) {
        Project p = new Project();

        p.setProject_name(project.getProject_name());
        p.setDept_id(project.getDept_id());
        p.setLeader(project.getLeader());
        p.setLevel(project.getLevel());
        p.setFunds(project.getFunds());
        p.setProfiles(project.getProfiles());

        return projectMapper.insert(p);
    }

    /**
     * 更新项目信息
     * 增加成员以及删除成员使用 成员表中的插入与删除实现
     * @param project
     * @return
     */
    @Override
    public int updateOne(UpdateProject project) {
        Project p = new Project();

        p.setProject_id(project.getProject_id());
        p.setProject_name(project.getProject_name());
        p.setDept_id(project.getDept_id());
        p.setLeader(project.getLeader());
        p.setLevel(project.getLevel());
        p.setFunds(project.getFunds());
        p.setProfiles(project.getProfiles());
        int cnt = projectMapper.updateById(p);

        List<Long> add = project.getAdd();
        List<Long> remove = project.getRemove();
        add.removeIf(Objects::isNull);
        remove.removeIf(Objects::isNull);

        if (!add.isEmpty()) {
            add.forEach(i -> {
                Member m = new Member();
                m.setProject_id(project.getProject_id());
                m.setUser_id(i);
                memberMapper.insert(m);
            });
        }

        if (!remove.isEmpty()) {
            remove.forEach(i -> {
                QueryWrapper<Member> wrapper = new QueryWrapper<>();
                wrapper.eq("project_id", project.getProject_id()).eq("user_id", i);
                memberMapper.delete(wrapper);
            });
        }

        return cnt;
    }

    /**
     * 删除项目
     * @param project_id 项目id
     * @return
     */
    @Override
    public int deleteOne(String project_id) {
        projectMapper.deleteById(project_id);
        memberMapper.delete(new QueryWrapper<Member>().eq("project_id",project_id));

        progressMapper.delete(new QueryWrapper<Progress>().eq("project_id",project_id));
        return 0;
    }
}




